<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>Demo Mail Application</title>

	<style type="text/css">
		@import "../../../dojo/resources/dojo.css";
		@import "../../../dijit/themes/soria/soria.css";
		@import "mail/mail.css";
	</style>

	<script type="text/javascript" src="../../../dojo/dojo.js"
		djConfig="isDebug: true, parseOnLoad: true, defaultTestTheme: 'soria'"></script>
	<script type="text/javascript" src="../../../dijit/tests/tests/_testCommon.js"></script>
	<script type="text/javascript">
		// Use profile builds, if available. Since we use pretty much all of
		// the widgets, just use dijit-all. A custom profile would provide some
		// additional savings.
		dojo.require("dijit.dijit");
		dojo.require("dijit.dijit-all");

		dojo.require("dojo.parser");
		dojo.require("dojo.data.ItemFileWriteStore");

		dojo.require("dijit.dijit");
		dojo.require("dijit.Declaration");
		dojo.require("dijit.form.Button");
		dojo.require("dijit.Menu");
		dojo.require("dijit.Tree");
		dojo.require("dijit.Tooltip");
		dojo.require("dijit.Dialog");
		dojo.require("dijit.Toolbar");
		dojo.require("dijit.Calendar");
		dojo.require("dijit.ColorPalette");
		dojo.require("dijit.Editor");
		dojo.require("dijit._editor.plugins.LinkDialog");
		dojo.require("dijit.ProgressBar");

		dojo.require("dijit.form.ComboBox");
		dojo.require("dijit.form.CheckBox");
		dojo.require("dijit.form.FilteringSelect");
		dojo.require("dijit.form.Textarea");

		dojo.require("dijit.layout.LayoutContainer");
		dojo.require("dijit.layout.SplitContainer");
		dojo.require("dijit.layout.AccordionContainer");
		dojo.require("dijit.layout.TabContainer");
		dojo.require("dijit.layout.ContentPane");
		dojo.require("dojox.xml.Script");



		var paneId=1;

		// for "new message" tab closing
		function testClose(pane,tab){
		  return confirm("Are you sure you want to leave your changes?");
		}

		// fake mail download code:
		var numMails;
		var updateFetchStatus = function(x){
			if(x == 0){
				dijit.byId('fakeFetch').update({ indeterminate: false });
				return;
			}
			dijit.byId('fakeFetch').update({ progress: x });
			if (x == numMails){
				dojo.fadeOut({ node: 'fetchMail', duration:800,
					// set progress back to indeterminate. we're cheating, because this
					// doesn't actually have any data to "progress"
					onEnd: function(){
						dijit.byId('fakeFetch').update({ indeterminate: true });
						dojo.byId('fetchMail').style.visibility='hidden'; // remove progress bar from tab order
					}
				}).play();
			}
		}
		var fakeReport = function(percent){
			// FIXME: can't set a label on an indeterminate progress bar
			// like if(this.indeterminate) { return " connecting."; }
			return "Fetching: "+(percent*this.maximum) + " of " + this.maximum + " messages.";
		}
		var fakeDownload = function(){
			dojo.byId('fetchMail').style.visibility='visible';
			numMails = Math.floor(Math.random()*10)+1;
			dijit.byId('fakeFetch').update({ maximum: numMails, progress:0 });
			dojo.fadeIn({ node: 'fetchMail', duration:300 }).play();
			for (var i=0; i<=numMails; i++){
				setTimeout("updateFetchStatus("+i+")",((i+1)*(Math.floor(Math.random()*100)+400)));
			}
		}
		// fake sending dialog progress bar
		var stopSendBar = function(){
			dijit.byId('fakeSend').update({indeterminate: false});
			dijit.byId('sendDialog').hide();
			tabs.selectedChildWidget.onClose = function(){return true;};  // don want confirm message
			tabs.closeChild(tabs.selectedChildWidget);
		}

		var showSendBar = function(){
			dijit.byId('fakeSend').update({ indeterminate: true });
			dijit.byId('sendDialog').show();
			setTimeout("stopSendBar()", 3000);
		}

	</script>
</head>
<body class="soria">
	<script type="text/xml" dojoType="dojox.xml.Script">
		<ui xmlns:dojo="dojo" xmlns="html">
			<dojo:data.ItemFileWriteStore jsId="mailStore" url="mail/mail.json"/>
		</ui>
	</script>

	<!-- Inline declaration of a table widget (thanks Alex!) -->
	<script type="text/xml" dojoType="dojox.xml.Script">
		<ui xmlns:dijit="dijit" xmlns:html="html" xmlns:dojo="dojo" xmlns="html">

		<table dojoType="dijit.Declaration"
			widgetClass="demo.Table" class="demoTable"
			defaults="{ store: null, query: { query: { type: 'message' } }, columns: [ { name: 'From', attribute: 'sender' }, { name: 'Subject', attribute: 'label' }, { name: 'Sent on', attribute: 'sent',
			format: function(v){ return dojo.date.locale.format(dojo.date.stamp.fromISOString(v), {selector: 'date'}); }
			} ] }">
			<thead dojoAttachPoint="head">
				<tr dojoAttachPoint="headRow"></tr>
			</thead>
			<tbody dojoAttachPoint="body">
				<tr dojoAttachPoint="row"></tr>
			</tbody>

			<html:script type="dojo/method">
			<![CDATA[
				dojo.forEach(this.columns, function(item, idx){
					var icn = item.className||"";
					// add a header for each column
					var tth = document.createElement("th");
					tth.innerHTML = "<span class='arrowNode'></span>"+ item.name;
					tth.className = icn;
					dojo.connect(tth, "onclick", dojo.hitch(this, "onSort", idx));
					this.headRow.appendChild(tth);

					// and fill in the column cell in the template row
					this.row.appendChild(document.createElement("td"));
					this.row.lastChild.className = icn;
				}, this);
				this.runQuery();
				]]>
			</html:script>
			<html:script type="dojo/method" event="onSort" args="index">
			<![CDATA[
				var ca = this.columns[index].attribute;
				var qs = this.query.sort;
				// clobber an existing sort arrow
				dojo.query("> th", this.headRow).removeClass("arrowUp").removeClass("arrowDown");
				if(qs && qs[0].attribute == ca){
					qs[0].descending = !qs[0].descending;
				}else{
					this.query.sort = [{
						attribute: ca,
						descending: false
					}];
				}
				var th = dojo.query("> th", this.headRow)[index];
				dojo.addClass(th, (this.query.sort[0].descending ? "arrowUp" : "arrowDown"));
				this.runQuery();
				]]>
			</html:script>
			<html:script type="dojo/method" event="runQuery">
			<![CDATA[
				this.query.onBegin = dojo.hitch(this, function(){ dojo.query("tr", this.body).orphan(); });
				this.query.onItem = dojo.hitch(this, "onItem");
				this.query.onComplete = dojo.hitch(this, function(){
					dojo.query("tr:nth-child(odd)", this.body).addClass("oddRow");
					dojo.query("tr:nth-child(even)", this.body).removeClass("oddRow");
				});
				this.store.fetch(this.query);
				]]>
			</html:script>
			<html:script type="dojo/method" event="onItem" args="item">
			<![CDATA[
				var tr = this.row.cloneNode(true);
				dojo.query("td", tr).forEach(function(n, i, a){
					var tc = this.columns[i];
					var tv = this.store.getValue(item, tc.attribute)||"";
					if(tc.format){ tv = tc.format(tv, item, this.store); }
					n.innerHTML = tv;
				}, this);
				this.body.appendChild(tr);
				dojo.connect(tr, "onclick", this, function(){ this.onClick(item); });
				]]>
			</html:script>
			<html:script type="dojo/method" event="onClick" args="item">
			<![CDATA[
				// user should override
				console.log("click on item " + item);
			]]>
			</html:script>
		</table>
		</ui>
	</script>

	<script type="text/xml" dojoType="dojox.xml.Script">
	<ui xmlns:dijit="dijit" xmlns:html="html" xmlns:dojo="dojo" xmlns="html">
	<!-- Inline declaration for programmatically created "New Message" tabs -->
	<dijit:Declaration widgetClass="mail.NewMessage">
		<dijit:layout.LayoutContainer dojoAttachPoint="container" title="Composing..." closeable="true">
			<dijit:layout.LayoutContainer layoutAlign="top" style="overflow: visible; z-index: 10; color:#666; ">
				<table width="100%">
				<tbody>
					<tr style="padding-top:5px;">
						<td style="padding-left:20px; padding-right: 8px; text-align:right;"><label for="${id}_to">To:</label></td>
						<td width="100%">
							<dijit:form.ComboBox id="${id}_to" value=" " hasDownArrow="false">
								<option></option>
								<option>adam@yahoo.com</option>
								<option>barry@yahoo.com</option>
								<option>bob@yahoo.com</option>
								<option>cal@yahoo.com</option>
								<option>chris@yahoo.com</option>
								<option>courtney@yahoo.com</option>
							</dijit:form.ComboBox>
						</td>
					</tr>
					<tr>
						<td style="padding-left: 20px; padding-right:8px; text-align:right;"><label for="${id}_subject">Subject:</label></td>
						<td width="100%">
							<dijit:form.ComboBox id="${id}_subject" value=" " hasDownArrow="false">
								<option></option>
								<option>progress meeting</option>
								<option>reports</option>
								<option>lunch</option>
								<option>vacation</option>
								<option>status meeting</option>
							</dijit:form.ComboBox>
						</td>
					</tr>
				</tbody>
				</table>
				<hr noshade="true" size="1"/>
			</dijit:layout.LayoutContainer>

			<!-- new messase part -->
			<dijit:layout.LayoutContainer  dojoType="dijit.layout.LayoutContainer" layoutAlign="client">
				<dijit:layout.ContentPane href="mail/newMail.xml"/>
			</dijit:layout.LayoutContainer>

			<dijit:layout.LayoutContainer  dojoType="dijit.layout.LayoutContainer" layoutAlign="bottom" align="center">
				<dijit:form.Button iconClass="mailIconOk" label="Send">
					<html:script type="dojo/method" event="onClick">
						var toField = dojo.byId("${id}_to");
						if(toField.value == ""){
							alert("Please enter a recipient address");
						}else{
							showSendBar();
						}
					</html:script>
				</dijit:form.Button>
				<dijit:form.Button iconClass="mailIconCancel" label="Cancel">
					<html:script type="dojo/method" event="onClick">
						tabs.closeChild(tabs.selectedChildWidget);
					</html:script>
				</dijit:form.Button>
			</dijit:layout.LayoutContainer>

		</dijit:layout.LayoutContainer>
	</dijit:Declaration>

	<dijit:layout.LayoutContainer id="main">
		<!-- toolbar with new mail button, etc. -->
		<dijit:Toolbar dojoType="dijit.Toolbar" layoutAlign="top" style="height:25px;">
			<dijit:form.ComboButton id="getMail"
				iconClass="mailIconGetMail" optionsTitle="Mail Source Options">
				<html:script type="dojo/method" event="onClick">
					 fakeDownload();
				</html:script>
				<span>Get Mail</span>
				<dijit:Menu dojoType="dijit.Menu">
					<dijit:MenuItem iconClass="mailIconGetMail">Yahoo</dijit:MenuItem>
					<dijit:MenuItem  iconClass="mailIconGetMail">GMail</dijit:MenuItem>
				</dijit:Menu>
			</dijit:form.ComboButton>
			<dijit:Tooltip connectId="getMail">Click to download new mail.</dijit:Tooltip>

			<dijit:form.Button
				id="newMsg"
				iconClass="mailIconNewMessage"
				label="New Message">
				<html:script type="dojo/method" event="onClick">
					/* make a new tab for composing the message */
					var newTab = new mail.NewMessage({id: "new"+paneId  }).container;
					dojo.mixin(newTab,
						{
							title: "New Message #" + paneId++,
							closable: true,
							onClose: testClose
						}
					);
					tabs.addChild(newTab);
					tabs.selectChild(newTab);
				</html:script>
			</dijit:form.Button>
			<dijit:Tooltip dojoType="dijit.Tooltip" connectId="newMsg">Click to compose new message.</dijit:Tooltip>

			<dijit:form.Button label="Options" iconClass="mailIconOptions">
				<html:script type="dojo/method" event="onClick">
								dijit.byId('optionsDialog').show();
				</html:script>
			</dijit:form.Button>
			<dijit:Tooltip connectId="options">Set various options</dijit:Tooltip>
		</dijit:Toolbar>

		<dijit:layout.TabContainer dojoType="dijit.layout.TabContainer" id="tabs" jsId="tabs" layoutAlign="client">
			<!-- main section with tree, table, and preview -->
			<dijit:layout.SplitContainer
				orientation="horizontal"
				sizerWidth="5"
				activeSizing="0"
				title="Inbox"
			>
				<dijit:layout.AccordionContainer sizeMin="20" sizeShare="20">
					<dijit:layout.AccordionPane title="Folders">
						<!--<span id="treePlaceHolder"
								style="background-color:#F00; color:#FFF;">
								Loading tree widget...
						</span>-->
						<dijit:Tree id="mytree" store="mailStore"
							labelAttr="label" childrenAttr="folders" query="{type:'folder'}" label="Folders">
							<html:script type="dojo/method" event="onClick" args="item">
								if(!item){
									return;	// top level node in tree doesn't correspond to any item
								}
								/* filter the message list to messages in this folder */
								table.query.query = {
									type: "message",
									folder: mailStore.getValue(item, "id")
								};
								table.runQuery();
							</html:script>
							<html:script type="dojo/method" event="getIconClass" args="item">
							<![CDATA[
								return (item && mailStore.getValue(item, "icon")) || "mailIconFolderDocuments";
							]]>
							</html:script>
						</dijit:Tree>
					</dijit:layout.AccordionPane>
					<dijit:layout.AccordionPane title="Address Book">
						<span dojoType="demo.Table" store="mailStore"
							query="{ query: { type: 'address' }, columns: [ {name: 'User name', attribute: 'label'} ], sort: [ { attribute: 'label' } ]  }"
							id="addresses" style="width: 100%" >
							<html:script type="dojo/method" event="preamble">
								this.query = { type: "address" };
								this.columns = [
									{
										name: "Name",
										attribute: "label"
									}
								];
							</html:script>
							<html:script type="dojo/method" event="onClick" args="item">
								table.query.query.sender = mailStore.getValue(item, "sender");
								delete table.query.query.folder;
								table.runQuery();
							</html:script>
						</span>
					</dijit:layout.AccordionPane>
				</dijit:layout.AccordionContainer>

				<dijit:layout.SplitContainer
					id="rightPane"
					orientation="vertical"
					sizerWidth="5"
					activeSizing="0"
					sizeMin="50" sizeShare="85"
				>
					<dijit:layout.ContentPane id="listPane" sizeMin="20" sizeShare="20">
						<span dojoType="demo.Table" store="mailStore"
							query="{ query: { type: 'message' }, sort: [ { attribute: 'label' } ]  }"
							id="foo" jsId="table" style="width: 100%">
							<html:script type="dojo/method" event="onClick" args="item">
							var sender = this.store.getValue(item, "sender");
								var subject = this.store.getValue(item, "label");
								var sent = dojo.date.locale.format(
									dojo.date.stamp.fromISOString(this.store.getValue(item, "sent")),
									{formatLength: "long", selector: "date"});
								var text = this.store.getValue(item, "text");

								var messageInner = "&lt;span class='messageHeader'&gt;From: "  + sender + "&lt;br&gt;" +
																	"Subject: "+ subject + "&lt;br&gt;" +
									"Date: " + sent + "&lt;br&gt;&lt;br&gt;&lt;/span&gt;" +
									text;
								dijit.byId("message").setContent(messageInner);
							</html:script>
						</span>
					</dijit:layout.ContentPane>
					<dijit:layout.ContentPane id="message" sizeMin="20" sizeShare="80">
						<p>
						This is a simple application mockup showing some of the dojo widgets:
						</p>
						<ul>
						  <li>layout widgets: SplitContainer, LayoutContainer, AccordionContainer</li>
						  <li>TooltipDialog, Tooltip</li>
						  <li>Tree</li>
						  <li>form widgets: Button, DropDownButton, ComboButton, FilteringSelect, ComboBox</li>
						  <li>Editor</li>
						</ul>
						<p>
						The message list above originally contains all the messages, but you can filter it by clicking on items in the left Accordion. Then click on the messages in the above list to display them. There's no server running, so the app is just a facade and it doesn't really do anything.
						<!-- TODO: delete button (we can delete since we are using ItemFileWriteStore -->
						</p>
						<p>
						<span style="font-family: 'Comic Sans MS',Textile,cursive; color: blue; font-style: italic;">-- Bill</span>
						</p>
					</dijit:layout.ContentPane>
				</dijit:layout.SplitContainer><!-- end of vertical SplitContainer -->
			</dijit:layout.SplitContainer><!-- end of horizontal SplitContainer -->
		</dijit:layout.TabContainer><!-- end of TabContainer -->

		<dijit:layout.ContentPane layoutAlign="bottom" id="footer" align="left">
			<span style="float:right;">DojoMail v1.0 (demo only)</span>
			<div id="fetchMail" style="opacity:0;visibility:hidden">
				<dijit:ProgressBar annotate="true" id="fakeFetch" style="height:15px; width:275px;" indeterminate="true" report="fakeReport"/>
			</div>
		</dijit:layout.ContentPane>
	</dijit:layout.LayoutContainer> <!-- end of Layoutcontainer -->

		<dijit:Dialog id="optionsDialog" title="Options:">
				<table>
				<tbody>
					<tr>
					<td style="text-align:right;">
						<label for="option1">Transport type:</label>
					</td>
					<td>
						<dijit:form.FilteringSelect id="option1" value=" ">
									<option value="pop3">POP3</option>
									<option value="imap">IMAP</option>
						</dijit:form.FilteringSelect>
					</td>
					</tr>
					<tr>
					<td style="text-align:right;">
						<label for="option2">Server:</label>
					</td>
					<td>
						<dijit:form.TextBox id="option2" type="text"/>
					</td>
					</tr>

					<tr>
					<td style="text-align:right;">
						<dijit:form.CheckBox type="checkbox" id="fooCB"/>
					</td>
					<td>
						<label for="fooCB">Leave messages on Server</label>
					</td>
					</tr>
					<tr>
					<td style="text-align:right;">
						<dijit:form.CheckBox type="checkbox" id="fooCB2"/>
					</td>
					<td>
						<label for="fooCB2">Remember Password</label>
					</td>
					</tr>

					<tr>
					<td colSpan="2" style="text-align:center;">
					<dijit:form.Button iconClass="mailIconOk" onclick="dijit.byId('optionsDialog').hide();">OK</dijit:form.Button>
					<dijit:form.Button iconClass="mailIconCancel" onclick="dijit.byId('optionsDialog').hide();">Abort</dijit:form.Button>
					</td>
					</tr>
				</tbody>
				</table>
		</dijit:Dialog>

		<dijit:Dialog dojoType="dijit.Dialog" id="sendDialog" title="Sending Mail">
			<dijit:ProgressBar id="fakeSend" style="height:15px; width:175px;" indeterminate="true" ></dijit:ProgressBar>
		</dijit:Dialog>
	</ui>
	</script>
</body>
</html>
